@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion

:: When SBM Notifications are enabled but no EMail server is configured, the 
::    Notification Engine writes a diagnostic line to the "ttmail.log" file 
::    every 10 seconds.  Obviously this clutters up the log, takes up space, 
::    and is an annoyance when trying to read usable diagnostic messages from
::    the log file.
:: This BATCH file will strip out those messages in a safe manner, meaning
::    that the Notification server is stopped while the file is cleaned up.
::    This script is probably for pre SBM 10.x only as it stops and starts
::    the SBM "Serena Broker Service".   

:: --------------------------------------------------------------

::  File:    C:\Program Files\Utils\cleanup_ttmail_log.cmd
::  Script:  cleanup_ttmail_log.cmd
::  Author:  paul_m_thompson@yahoo.com

::  Date:       $Date: 2013-08-26 16:27:04 +0000 (Mon, 26 Aug 2013) $
::  Revision:   $Revision: 79 $

:: --------------------------------------------------------------


Set ttmail.log=C:\Program Files\Serena\SBM\Application Engine\log\ttmail.log
Set ttmail.tmp=C:\Program Files\Serena\SBM\Application Engine\log\ttmail.log.tmp
Set SerenaBrokerSvc=Serena Broker Service


echo Stopping "%SerenaBrokerSvc%"
sc stop "%SerenaBrokerSvc%" >nul 2>&1
Call :WaitForStop
If %ERRORLEVEL% neq 0 (
   Echo Error code back from :WaitForStop
   Echo We're outta here.
   Exit /B 1 
)

Echo Strip "No e-mail server is configured..." lines
grep -vF "No e-mail server is configured, please use the TeamTrack Administrator to configure an e-mail server; mail not sent"  "%ttmail.log%" > "%ttmail.tmp%"
copy /Y /V "%ttmail.tmp%" "%ttmail.log%" >nul 2>&1
del /F /Q "%ttmail.tmp%"

echo Restarting "%SerenaBrokerSvc%"
sc start "%SerenaBrokerSvc%" >nul 2>&1

Exit /B 0

:: --------------------------------------------------------------------------------
:WaitForStop

echo Check status of %SerenaBrokerSvc%
For /F "skip=3 tokens=1,3,4" %%A In ('sc query "%SerenaBrokerSvc%"') Do (
   rem :: A=STATE   B=4  C=RUNNING
   rem :: A=STATE   B=3  C=STOP_PENDING
   rem :: A=STATE   B=2  C=START_PENDING
   rem :: A=STATE   B=1  C=STOPPED
   If /I "%%A:%%C" equ "STATE:STOPPED" Goto :Stopped
)

:: Wait 1 second then try again
Echo ... wait 1 second ...
ping -n 1 -w 1000 192.168.255.1  >nul 2>&1
Goto :WaitForStop

:Stopped 
Echo Detected that the service is stopped
Exit /B 0



